﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace SiteFox.Business.Manager
{
    public static class Statistics
    {
        public static void Create()
        {
            Model.Statistics _Statistics = new Model.Statistics();
            _Statistics.DateTime = DateTime.Now;
            _Statistics.Guid = HttpContext.Current.Session["_VisitorGuid"].ToString();
            string _TempBrowser = HttpContext.Current.Request.ServerVariables["HTTP_USER_AGENT"].ToString();
            _Statistics.Browser = FindBrowser(_TempBrowser);
            _Statistics.IP = HttpContext.Current.Request.UserHostAddress.ToString();
            _Statistics.Page = HttpContext.Current.Request.Url.ToString();
            _Statistics.OS = FindOS(HttpContext.Current.Request.Browser.Platform.ToString(), _TempBrowser);
            if (HttpContext.Current.Request.UrlReferrer != null)
            {
                _Statistics.Ref = HttpContext.Current.Request.UrlReferrer.ToString();
            }
            else
            {
                _Statistics.Ref = "Direct Visit";
            }
            var _Entities = new Model.Entities(Data.WebConfig.GetConnectionString());
            _Entities.AddToStatistics(_Statistics);
            _Entities.SaveChanges();
        }        
        public static IList<Model.Statistics> GetAll()
        {
            var _Entities = new Model.Entities(Data.WebConfig.GetConnectionString());
            var _Query = from c in _Entities.Statistics select c;
            return _Query.ToList();
        }
        public static Model.Statistics GetByID(int _ID)
        {
            var _Entities = new Model.Entities(Data.WebConfig.GetConnectionString());
            var _Query = from c in _Entities.Statistics where c.ID == _ID select c;
            if (_Query.Count() > 0)
            {
                return _Query.Single();
            }
            else
            {
                return null;
            }
        }
        //public static IList<Model.Statistics> Visits(DateTime StartDate, DateTime EndDate)
        //{
        //    var _Entities = new Model.Entities(Data.ConnectionString.GetEntityConnectionString());
        //    var _Query = from c in _Entities.Statistics where (c.DateTime >= StartDate && c.DateTime <= EndDate) select c;
        //    return _Query.GroupBy(
        //}
        //public static IList<Model.Statistics> Pageviews(DateTime StartDate, DateTime EndDate)
        //{
        //    var _Entities = new Model.Entities(Data.ConnectionString.GetEntityConnectionString());
        //    var _Query = from c in _Entities.Statistics where c.ID == _ID select c;
        //    return _Query.ToList();
        //}
        //public static IList<Model.Statistics> PageReferrer(DateTime StartDate, DateTime EndDate)
        //{
        //    var _Entities = new Model.Entities(Data.ConnectionString.GetEntityConnectionString());
        //    var _Query = from c in _Entities.Statistics where c.ID == _ID select c;
        //    return _Query.ToList();
        //}
        //public static IList<Model.Statistics> Browsers(DateTime StartDate, DateTime EndDate)
        //{
        //    var _Entities = new Model.Entities(Data.ConnectionString.GetEntityConnectionString());
        //    var _Query = from c in _Entities.Statistics where c.ID == _ID select c;
        //    return _Query.ToList();
        //}
        //public static IList<Model.Statistics> OSs(DateTime StartDate, DateTime EndDate)
        //{
        //    var _Entities = new Model.Entities(Data.ConnectionString.GetEntityConnectionString());
        //    var _Query = from c in _Entities.Statistics where c.ID == _ID select c;
        //    return _Query.ToList();
        //}
        //public static IList<Model.Statistics> IPs(DateTime StartDate, DateTime EndDate)
        //{
        //    var _Entities = new Model.Entities(Data.ConnectionString.GetEntityConnectionString());
        //    var _Query = from c in _Entities.Statistics where c.ID == _ID select c;
        //    return _Query.ToList();
        //}

        private static string FindBrowser(string Http_User_Agent)
        {
            string _Browser;
            if (Http_User_Agent.Contains("Chrome"))
            {
                _Browser = "Chrome";
            }
            else if (Http_User_Agent.Contains("Avant Browser"))
            {
                _Browser = "Avant Browser";
            }
            else if (Http_User_Agent.Contains("Googlebot"))
            {
                _Browser = "Googlebot";
            }
            else if (Http_User_Agent.Contains("Yahoo! Slurp"))
            {
                _Browser = "Yahoo! Slurp";
            }
            else if (Http_User_Agent.Contains("Mediapartners-Google"))
            {
                _Browser = "Mediapartners-Google";
            }
            else if (Http_User_Agent.Contains("msnbot"))
            {
                _Browser = "msnbot";
            }
            else if (Http_User_Agent.Contains("SurveyBot"))
            {
                _Browser = "SurveyBot/2.3 (Whois Source)";
            }
            else if (Http_User_Agent.Contains("Baiduspider"))
            {
                _Browser = "Baiduspider";
            }
            else if (Http_User_Agent.Contains("FeedFetcher-Google"))
            {
                _Browser = "FeedFetcher-Google";
            }
            else if (Http_User_Agent.Contains("ia_archiver"))
            {
                _Browser = "ia_archiver";
            }
            else
            {
                _Browser = HttpContext.Current.Request.Browser.Browser.ToString();
                _Browser += " " + HttpContext.Current.Request.Browser.Version.ToString();
            }
            return _Browser;
        }
        private static string FindOS(string PlatformInfo, string Http_User_Agent)
        {
            string _OS = "";
            if (PlatformInfo.Contains("Win"))
            {
                if (Http_User_Agent.Contains("Windows NT 6.1"))
                {
                    _OS = "Windows 7";
                }
                if (Http_User_Agent.Contains("Windows NT 6.0"))
                {
                    if (Http_User_Agent.Contains("Media Center PC 5.0"))
                    {
                        _OS = "Windows Vista";
                    }
                    else
                    {
                        _OS = "Windows 2008 Server";
                    }
                }
                if (Http_User_Agent.Contains("Windows NT 5.2"))
                {
                    _OS = "Windows 2003 Server/ XP 64-BIT";
                }
                if (Http_User_Agent.Contains("Windows NT 5.1"))
                {
                    _OS = "Windows XP";
                }
                if (Http_User_Agent.Contains("Windows NT 5.01"))
                {
                    _OS = "Windows 2000 SP1";
                }
                if (Http_User_Agent.Contains("Windows NT 5.0"))
                {
                    _OS = "Windows 2000";
                }
                if (Http_User_Agent.Contains("Windows NT 4.0"))
                {
                    _OS = "Windows NT 4.0";
                }
                if (Http_User_Agent.Contains("Windows 98"))
                {
                    _OS = "Windows 98";
                }
                if (Http_User_Agent.Contains("Windows 95"))
                {
                    _OS = "Windows 95";
                }
                if (Http_User_Agent.Contains("Windows CE"))
                {
                    _OS = "Windows CE";
                }
            }
            else if (PlatformInfo.Contains("Mac OS X"))
            {
                if (Http_User_Agent.Contains("iPhone"))
                {
                    _OS = "iPhone-Mac OS X";
                }
                else
                {
                    _OS = "Mac OS X";
                }
            }
            else if (PlatformInfo.Contains("UNIX"))
            {
                if (Http_User_Agent.Contains("FreeBSD"))
                {
                    _OS = "FreeBSD";
                }
                else if (Http_User_Agent.Contains("Linux"))
                {
                    if (Http_User_Agent.Contains("Ubuntu"))
                    {
                        _OS = "Ubuntu";
                    }
                    else if (Http_User_Agent.Contains("Fedora"))
                    {
                        _OS = "Fedora";
                    }
                    else if (Http_User_Agent.Contains("CentOS"))
                    {
                        _OS = "CentOS";
                    }
                    else if (Http_User_Agent.Contains("Red Hat"))
                    {
                        _OS = "Red Hat";
                    }
                    //add more linux versions here
                    else
                    {
                        _OS = "Linux";
                    }
                }
                else
                {
                    _OS = "UNIX";
                }
            }
            else if (PlatformInfo.Contains("Unknown"))
            {
                _OS = "Unknown";
            }
            else 
            {
                _OS = "Unknown";
            }
            return _OS;
        }
    }
}
